package com.wclass.controller.app;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.wclass.common.ReturnObject;
import com.wclass.entity.CourseType;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.ICourseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程分类 前端控制器
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-20
 */
@Api(description = "课程分类管理")
@RestController
@RequestMapping("/courseType")
public class CourseTypeController {
    @Autowired
    private ICourseTypeService iCourseTypeService;

    @ApiOperation("获取一级和二级分类-首次登陆使用")
    @IgnoreAuth
    @RequestMapping(value = "/getIndexCourseTypeList", method = RequestMethod.POST)
    public ReturnObject<List<CourseType>> getIndexCourseTypeList() {
        List<CourseType> list = iCourseTypeService.selectList(new EntityWrapper<CourseType>().eq("p_id", 0));
        for (CourseType info : list) {
            info.setList(iCourseTypeService.selectList(new EntityWrapper<CourseType>().eq("p_id", info.getId())));
            for (CourseType type : info.getList()) {
                type.setList(iCourseTypeService.selectList(new EntityWrapper<CourseType>().eq("p_id", type.getId())));
            }
        }
        return new ReturnObject<>(0, "获取成功", list);
    }

    @ApiOperation("获取一级分类")
    @IgnoreAuth
    @RequestMapping(value = "/getOneCourseTypeList", method = RequestMethod.POST)
    public ReturnObject<List<CourseType>> getOneCourseTypeList() {
        return new ReturnObject<>(0, "获取成功", iCourseTypeService.selectList(new EntityWrapper<CourseType>().eq("p_id",
                0)));
    }

    @ApiOperation("获取二、三级分类")
    @IgnoreAuth
    @RequestMapping(value = "/getTwoCourseTypeList", method = RequestMethod.POST)
    public ReturnObject<List<CourseType>> getTwoCourseTypeList(@ApiParam("上级分类id") @RequestParam Long pId) {
        return new ReturnObject<>(0, "获取成功", iCourseTypeService.selectList(new EntityWrapper<CourseType>().eq(
                "p_id", pId)));
    }

    @IgnoreAuth
    @ApiOperation("获取各级分类名称")
    @RequestMapping(value = "/getCourseTypeNames", method = RequestMethod.POST)
    public ReturnObject<Object> getCourseTypeNames(@ApiParam("三级分类id") @RequestParam Long courseTypeId){
        Map<String, Object> map = Maps.newHashMap();
        CourseType trdCourseType = iCourseTypeService.selectById(courseTypeId);
        map.put("thirdName", trdCourseType.getName());

        CourseType sedCourseType = iCourseTypeService.selectById(trdCourseType.getPId());
        map.put("secondName", sedCourseType.getName());

        CourseType firstCourseType = iCourseTypeService.selectById(sedCourseType.getPId());
        map.put("firstName", firstCourseType.getName());

        List<CourseType> trdCourseTypeList = iCourseTypeService.selectList(new EntityWrapper<CourseType>().eq("p_id", trdCourseType.getPId()));
        map.put("thirdList", trdCourseTypeList);

        return new ReturnObject<>(0, "获取成功", map);
    }

}

